package sg.edu.nus.iss.team8s.vmcs.control;

import sg.edu.nus.iss.team8s.vmcs.cache.*;
import sg.edu.nus.iss.team8s.vmcs.PropertyLoader.CashPropertyLoader;
import sg.edu.nus.iss.team8s.vmcs.PropertyLoader.DrinkPropertyLoader;
import sg.edu.nus.iss.team8s.vmcs.PropertyLoader.PropertyLoader;
import sg.edu.nus.iss.team8s.vmcs.beans.*;

import java.util.Observable;
import java.util.Observer;
/**
 * 
 * @author Zheng Xiaofeng, Nie Xiaoyi
 *
 */
public class StoreController implements Observer{
	
	private static StoreController instance = null;
	
	private PropertyLoader cashLoader;
	private PropertyLoader drinkLoader;
	private Store cashStore;
	private Store drinkStore;
	
	private StoreController(PropertyLoader cashLoader,PropertyLoader drinkLoader){
		this.cashLoader = cashLoader;
		this.drinkLoader = drinkLoader;
	}
	
	private StoreController(){
		
	}
	
	public static synchronized StoreController getInstance(){

		if(instance == null){
			instance = new StoreController();
		}
		return instance;
	}
	
	public static synchronized StoreController getInstance(PropertyLoader cashLoader,PropertyLoader drinkLoader){

		if(instance == null){
			instance = new StoreController(cashLoader,drinkLoader);
		}
		return instance;
	}
	
	public static void changeStoreQuantity(int type, int id, int value){
		StoreItem item;
		if(type ==1)
		{
			item = DrinksStore.getInstance().getItem(id);
			//change the store quantity
			item.setQuantity(value);
			DrinksStore.getInstance().updateItem(id, item); //update the item in cache and notify the observers
		}
		else{
			item = CashStore.getInstance().getItem(id);
			//change the store quantity
			item.setQuantity(value);
			CashStore.getInstance().updateItem(id, item); //update the item in cache and notify the observers
		}
	}
	
	public boolean dispenseDrink(int selected_brand){
		/*StoreItem drinkItem = DrinksStore.getInstance().getItem(selected_brand);
		//dispense Drink
		int qty = drinkItem.getQuantity() - 1;
		drinkItem.setQuantity(qty);
		//get the drinkItem from DrinksStore, decrement the quantity and notify the observers
		DrinksStore.getInstance().notifyObservers(drinkItem);
		return true;*/
		
		DrinksStore drinksStore = DrinksStore.getInstance();
		StoreItem drinkItem = drinksStore.getItem(selected_brand);
		drinkItem.decrement();
		drinksStore.updateItem(selected_brand, drinkItem);
		return true;
	}

	
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
	}
	
	public void initialize(){
		if(cashStore == null)
			cashStore = CashStore.getInstance();
		if(drinkStore == null)
			drinkStore = DrinksStore.getInstance();
		initializeStore();
	}
	
	private void initializeStore(){
		initializeCashStore();
		initializeDrinkStore();
	}
	
	private void initializeCashStore(){
		int num = cashLoader.getNumOfItems();
		cashStore.setStoreSize(num);
		for(int i = 1;i<=num;i++){
			StoreItem item = cashLoader.getItem(i);
			cashStore.addItem(i, item);
		}
	}
	
	private void initializeDrinkStore(){
		int num = drinkLoader.getNumOfItems();
		drinkStore.setStoreSize(num);
		for(int i = 1;i<=num;i++){
			StoreItem item = drinkLoader.getItem(i);
			/*StoreObject drinkBrand = item.getContent();
			String name = drinkBrand.getName();
			drinkBrand = drinkStore.findObject(name);
			if( drinkBrand != null){
				item.setContent(drinkBrand);
			}*/
			drinkStore.addItem(i, item);
		}
	}
	
	public boolean giveChange(int index,int number_coins){
		CashStoreItem item = (CashStoreItem)cashStore.getItem(index);
		for(int i=0;i<number_coins;i++){
			item.decrement();
		}
		cashStore.updateItem(index, item);
		return true;
	}
	
	public boolean storeCoin(Coin coin){
		CashStore cashStore = CashStore.getInstance();
		int index = cashStore.findCashStoreIndex(coin);
		CashStoreItem item = (CashStoreItem)cashStore.getItem(index);
		item.increment();
		cashStore.updateItem(index, item);
		return true;
	}
	
	public void saveCashProperty(){
		CashPropertyLoader cashProperty = CashPropertyLoader.getInstance("");
		cashProperty.setNumOfItems(cashStore.getStoreSize());
		for(int i=0;i<cashStore.getStoreSize();i++){
			CashStoreItem cashItem = (CashStoreItem)cashStore.getItem(i);
			cashProperty.setItem(i+1, cashItem);
		}
		
		cashProperty.saveProperty();
	}
	
	public void saveDrinksProperty(){
		DrinkPropertyLoader drinkProperty = DrinkPropertyLoader.getInstance("");
		drinkProperty.setNumOfItems(drinkStore.getStoreSize());
		for(int i=0;i<drinkStore.getStoreSize();i++){
			DrinksStoreItem drinkItem = (DrinksStoreItem)drinkStore.getItem(i);
			drinkProperty.setItem(i+1, drinkItem);
		}
		
		drinkProperty.saveProperty();
	}
	
	public void closeDown(){
		saveCashProperty();
		saveDrinksProperty();
	}
	
}
